use rusqlite::Connection;
use crate::device::Device::Device;
use chrono::{DateTime, NaiveDate, NaiveDateTime, NaiveTime};
use chrono::format::ParseError;
pub fn get_all() -> rusqlite::Result<Vec<Device>> {
    println!("begin get all devices ...");
    let conn = Connection::open("modbus_addrs.db")?;
    let mut stmt = conn.prepare(
        "SELECT d.id,d.ip, d.port,d.slave_id,d.create_user,d.create_time,d.last_update_time,d.last_update_user from devices d",
    )?;
    let mut rows = stmt.query([])?;
    let mut devices = Vec::new();
    while let Some(row) = rows.next()? {
        devices.push(Device {
            id: row.get(0)?,
            ip: row.get(1)?,
            port: row.get(2)?,
            slave_id: row.get(3)?,
            create_user: row.get(4)?,
            // create_time: NaiveDateTime::parse_from_str(row.get(5)?),
            // last_update_time: row.get(6)?,
            last_update_user: row.get(7)?,
        });
    }

    Ok(devices)
    
}
